﻿using ASmile.Com;
using ASmile.Extend;
using ASmile.ORM.Entitys;
using ASmile.Win;
using System;

namespace ASmile.Win
{
    /// <summary>
    /// crud的泛型支持，如果 TListModel 与 TSaveModel 不一致，则需要重写 CreateSaveModel方法
    /// </summary>
    /// <typeparam name="TListModel">显示的实体类型</typeparam>
    public partial class FrmCrud<TArgPageData, TSaveModel, TListModel> : FrmCrud
         where TListModel : class, new()
         where TSaveModel : class, new()
         where TArgPageData : ArgPageData, new()
    {
        public FrmCrud()
        {
            InitializeComponent();
        }

        protected virtual string BLLObjectName { set; get; }
        /// <summary>
        /// BLL对象 必须重写
        /// </summary>
        protected virtual IBLLCrud<TArgPageData, TSaveModel, TListModel> BLLObj { get; private set; }

        protected TArgPageData SearchArg { get; } = new TArgPageData();

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            if (!DesignMode)
            {
                if (BLLObj == null)
                {
                    if (BLLObjectName.IsEmpty())
                    {
                        System.Windows.Forms.MessageBox.Show("未正确配置BLL对象");
                        return;
                    }
                    BLLObj = ObjectFactory.GetObject<IBLLCrud<TArgPageData, TSaveModel, TListModel>>(BLLObjectName);
                }
            }
        }

        /// <summary>
        /// 创建保存对象的方法
        /// </summary>
        public override object CreateSaveModel()
        {
            return new TSaveModel();
        }

        /// <summary>
        /// 创建更新窗体的方法（如果 CreateAddForm 有实现，且编辑框一致则不用重写，否则必需重写）
        /// </summary>
        protected override FrmBaseEdit CreateEditForm()
        {
            return CreateAddForm();
        }
        /// <summary>
        /// 获取数据源的方法
        /// </summary>
        protected override object GetDataSource(int pageNo, int pageSize, string searchVal, out int rowCount)
        {
            SearchArg.PageNo = pageNo;
            SearchArg.PageSize = pageSize;
            SearchArg.SearchVal = searchVal;
            var ret = BLLObj.GetList(SearchArg);
            rowCount = ret.count;
            return ret.data;
        }
        /// <summary>
        /// 新增处理
        /// </summary>
        protected override ApiResult<string> AddSaveHandle(object m)
        {
            return BLLObj.Add(m as TSaveModel);
        }
        /// <summary>
        /// 更新处理
        /// </summary>
        protected override ApiResult<string> EditSaveHandle(object m)
        {
            if (!(m is TSaveModel saveModel))
            {
                saveModel = new TSaveModel();
                Helpers.ReflectionHelper.CopyObject(saveModel, m);
            }
            return BLLObj.Update(saveModel);
        }
        /// <summary>
        /// 删除处理
        /// </summary>
        protected override ApiResult DelHandle(object m)
        {
            if (!(m is TSaveModel saveModel))
            {
                saveModel = new TSaveModel();
                Helpers.ReflectionHelper.CopyObject(saveModel, m);
            }
            return BLLObj.Del(saveModel);
        }
        /// <summary>
        /// 高级搜索点击
        /// </summary>
        protected override void AdSearch_Click(object sender, EventArgs e)
        {
            ADSearch(SearchArg, BindDataSource);
        }

        protected override void BtnSearch_Click(object sender, EventArgs e)
        {
            //点击搜索取消高级搜索的数据
            SearchArg.ClearADList();
            base.BtnSearch_Click(sender, e);
        }
    }
}